package com.ruoyi.framework.interceptor;


import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.PageUtils;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.ognl.MapElementsAccessor;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/13
 **/
@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
@Component
public class MyPageInterceptor implements Interceptor {

    private Integer pageSize;

    private Integer pageNum;

    private String dbType;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 获取被代理的对象
        StatementHandler target = (StatementHandler) invocation.getTarget();
        // 获取要执行的sql包
        BoundSql boundSql = target.getBoundSql();
        Object parameterObject = boundSql.getParameterObject();
        PageDomain pageDomain = null;
        if (parameterObject instanceof PageDomain) {
            // 如果当前查询的接口是实现了
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        //如果项目中分页的pageSize是统一的，也可以在这里统一配置和获取，这样就不用每次请求都传递pageSize参数了。参数是在配置拦截器时配置的。
        String limit1 = properties.getProperty("limit", "10");
        this.pageSize = Integer.valueOf(limit1);
        this.dbType = properties.getProperty("dbType", "mysql");
    }
}
